package tom.client.stock;

import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

import tom.client.DataForm;
import tom.client.Library;
import tom.client.LookupDialog;
import tom.shared.Product;
import tom.shared.StkAdjust;
import tom.shared.Stock;

import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.IntegerBox;

public class FormStockLoc extends DataForm {

	private static FormStockLoc panel;

	public static FormStockLoc getPanel() {
		if (panel == null)
			panel = GWT.create(FormStockLoc.class);
		return panel;
	}

	protected Product record;
	protected List<StkAdjust> stkAdjusts;

	private final ListDataProvider<Stock> itemData = new ListDataProvider<Stock>();
	private final CellTable<Stock> cellTable;
	private final ListDataProvider<StkAdjust> stkAdjustData = new ListDataProvider<StkAdjust>();
	private final CellTable<StkAdjust> stkAdjustTable;
	private final Label lbTotal;
	private final IntegerBox qty;
	private final TextBox fromStock;
	private final TextBox toStock;
	private final Label invNo;

	public void setInvNo(String invNo) {
		this.invNo.setText(invNo);		
		Library.getStkService().selectStkAdjustByInvoice(this.invNo.getText(),
				txtCode.getText(), stkAdjCallback);
	}

	// private SingleSelectionModel<InvItem> itemSelection = new
	// SingleSelectionModel<InvItem>();

	public FormStockLoc() {
		setSize("800px", "500px");
		lbCode.setText("รหัสสินค้า");

		Label lblNewLabel = new Label("ชื่อสินค้า");
		add(lblNewLabel, 23, 111);

		txtCustomer = new TextBox();
		txtCustomer.setReadOnly(true);
		add(txtCustomer, 122, 111);
		txtCustomer.setSize("384px", "16px");

		ScrollPanel scrollPanel = new ScrollPanel();
		add(scrollPanel, 22, 236);
		scrollPanel.setSize("276px", "254px");

		cellTable = new CellTable<Stock>();
		cellTable.setStyleName("table");
		scrollPanel.setWidget(cellTable);
		cellTable.setPageSize(10);
		cellTable.setTableLayoutFixed(true);
		cellTable.setAccessKey('x');
		cellTable.setSize("100%", "100%");
		itemData.addDataDisplay(cellTable);

		/*
		 * TextColumn<InvItem> colProCode = new TextColumn<InvItem>() {
		 * 
		 * @Override public String getValue(InvItem object) { return
		 * object.proCode; } }; colProCode.setCellStyleNames("table");
		 * cellTable.addColumn(colProCode, "Code");
		 * cellTable.setColumnWidth(colProCode, "120px");
		 */

		TextColumn<Stock> colStkCode = new TextColumn<Stock>() {
			@Override
			public String getValue(Stock object) {
				return object.stkCode;
			}
		};
		colStkCode.setCellStyleNames("table");
		cellTable.addColumn(colStkCode, "คลัง#");
		cellTable.setColumnWidth(colStkCode, "82px");

		TextColumn<Stock> colBalance = new TextColumn<Stock>() {
			@Override
			public String getValue(Stock object) {
				return object.balance.toString();
			}
		};
		colBalance.setCellStyleNames("table");
		cellTable.addColumn(colBalance, "คงเหลือ");
		cellTable.setColumnWidth(colBalance, "79px");

		TextColumn<Stock> colRBalance = new TextColumn<Stock>() {
			@Override
			public String getValue(Stock object) {
				return object.rbalance.toString();
			}
		};
		colRBalance.setCellStyleNames("table");
		cellTable.addColumn(colRBalance, "คงเหลือ R");
		cellTable.setColumnWidth(colRBalance, "87px");

		// TextColumn<Stock> colPriority = new TextColumn<Stock>() {
		// @Override
		// public String getValue(Stock object) {
		// return object.priority.toString();
		// }
		// };
		// colPriority.setCellStyleNames("table");
		// cellTable.addColumn(colPriority, "Priority");
		// cellTable.setColumnWidth(colPriority, "100px");
		//
		Button btnNewButton = new Button("ปรับจำนวน");
		btnNewButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (FormStockLoc.this.getMode() == 1) {					
					Library.getStkService().updateStock(txtCode.getText(),
							invNo.getText(), fromStock.getText(),
							toStock.getText(), qty.getValue(), stkCallback);
				} else
					Window.alert("ไม่มีข้อมูลสินค้า");
			}
		});
		add(btnNewButton, 509, 167);

		Label lblNewLabel_1 = new Label("จำนวนรวม");
		add(lblNewLabel_1, 23, 212);

		lbTotal = new Label("0");
		add(lbTotal, 110, 212);

		fromStock = new TextBox();
		add(fromStock, 104, 167);
		fromStock.setSize("66px", "16px");

		Label lblNewLabel_2 = new Label("จากคลัง");
		add(lblNewLabel_2, 23, 167);

		Label lblNewLabel_3 = new Label("ย้ายไปคลัง");
		add(lblNewLabel_3, 203, 167);

		toStock = new TextBox();
		add(toStock, 283, 167);
		toStock.setSize("59px", "16px");

		qty = new IntegerBox();
		add(qty, 409, 167);
		qty.setSize("66px", "16px");

		Label lblNewLabel_4 = new Label("จำนวน");
		add(lblNewLabel_4, 367, 167);
		
		Label lblNewLabel_5 = new Label("Invoice#");
		add(lblNewLabel_5, 280, 212);
		
		invNo = new Label("");
		add(invNo, 367, 212);

		ScrollPanel scrollPanel_1 = new ScrollPanel();
		add(scrollPanel_1, 304, 236);
		scrollPanel_1.setSize("496px", "254px");

		stkAdjustTable = new CellTable<StkAdjust>();
		stkAdjustTable.setTableLayoutFixed(true);
		stkAdjustTable.setStyleName("table");
		stkAdjustTable.setPageSize(10);
		stkAdjustTable.setAccessKey('x');
		scrollPanel_1.setWidget(stkAdjustTable);
		stkAdjustTable.setSize("493px", "100%");
		stkAdjustData.addDataDisplay(stkAdjustTable);

		TextColumn<StkAdjust> textColumn = new TextColumn<StkAdjust>() {
			public String getValue(StkAdjust object) {
				return object.fromStock;
			}
		};
		stkAdjustTable.addColumn(textColumn, "From");
		stkAdjustTable.setColumnWidth(textColumn, "95px");

		TextColumn<StkAdjust> textColumn_1 = new TextColumn<StkAdjust>() {
			public String getValue(StkAdjust object) {
				return object.toStock;
			}
		};
		stkAdjustTable.addColumn(textColumn_1, "To");
		stkAdjustTable.setColumnWidth(textColumn_1, "82px");

		TextColumn<StkAdjust> textColumn_2 = new TextColumn<StkAdjust>() {
			public String getValue(StkAdjust object) {
				return object.qty.toString();
			}
		};
		stkAdjustTable.addColumn(textColumn_2, "จำนวน");
		stkAdjustTable.setColumnWidth(textColumn_2, "79px");
		
		TextColumn<StkAdjust> textColumn_3 = new TextColumn<StkAdjust>() {
			public String getValue(StkAdjust object) {
				return object.creUser;
			}
		};
		stkAdjustTable.addColumn(textColumn_3, "ผู้แก้ไข");
		stkAdjustTable.setColumnWidth(textColumn_3, "81px");
		
		TextColumn<StkAdjust> textColumn_4 = new TextColumn<StkAdjust>() {
			public String getValue(StkAdjust object) {
				return Library.DATETIME_FORMAT.format(object.creTime);
			}
		};
		stkAdjustTable.addColumn(textColumn_4, "วันเวลา");
	}

	@Override
	protected void search() {
		LookupDialog.show("เลือกสินค้าจากชื่อสินค้า", "proCode", txtCode);
	}

	@Override
	protected void refresh() {
		Library.getStkService().selectProduct(txtCode.getText(), dataCallback);
		Library.getStkService().selectStkAdjustByInvoice(FormStockLoc.this.invNo.getText(),
				txtCode.getText(), stkAdjCallback);
	}

	@Override
	protected void update() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void insert() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void delete() {

	}

	@Override
	protected void clearFields() {
		super.clearFields();
		txtCustomer.setText(null);
		itemData.getList().clear();
		itemData.refresh();
		fromStock.setText(null);
		toStock.setText(null);
		qty.setText(null);
		invNo.setText(null);
		stkAdjustData.getList().clear();
		stkAdjustData.refresh();
	}

	@Override
	protected void setFields() {
		super.setFields();
		if (record != null) {
			txtCustomer.setText(record.name);
			itemData.setList(record.stocks);
			stkAdjustData.setList(stkAdjusts);
			lbTotal.setText(String.valueOf(record.getBalance()));
			fromStock.setText(null);
			toStock.setText(null);
			qty.setText(null);
		} 
		
		//else
		//	clearFields();
	}

	@Override
	protected void getFields() {

	}

	@Override
	protected String getReportFile() {
		return null;
	}

	@Override
	protected Map<String, Object> genReportParams() {
		return null;
	}

	AsyncCallback<Product> dataCallback = new AsyncCallback<Product>() {

		@Override
		public void onSuccess(Product result) {
			record = result;
			setFields();

		}

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}
	};

	AsyncCallback<List<StkAdjust>> stkAdjCallback = new AsyncCallback<List<StkAdjust>>() {

		@Override
		public void onSuccess(List<StkAdjust> result) {
			stkAdjusts = result;
			setFields();
		}

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}
	};

	protected AsyncCallback<Boolean> stkCallback = new AsyncCallback<Boolean>() {
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}

		@Override
		public void onSuccess(Boolean result) {
			refresh();
			Library.getStkService().selectStkAdjustByInvoice(invNo.getText(),
					txtCode.getText(), stkAdjCallback);
		}
	};
	private final TextBox txtCustomer;
}
